home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / WHERE.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  133 lines

  1. /*
  2. **  WHERE.C:  will search all DIRs on the given drive for specified file.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8. #include <conio.h>
  9. #include <string.h>
  10.  
  11. #if defined(__ZTC__)
  12.  #include <direct.h>
  13.  #define GetDrive(d) dos_getdrive(&d)
  14.  #define SetDrive(d) {unsigned x;dos_setdrive(d,&x);}
  15.  #define FAR _far
  16. #elif defined(__TURBOC__)
  17.  #include <dir.h>
  18.  #define GetDrive(d) ((d) = getdisk() + 1)
  19.  #define SetDrive(d) (setdisk(d - 1))
  20.  #define FAR far
  21.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  22.  #define _dos_findnext(b) findnext(b)
  23.  #define find_t ffblk
  24.  #define _A_SUBDIR FA_DIREC
  25.  #define attrib ff_attrib
  26.  #define name ff_name
  27. #else /* assume MSC */
  28.  #include <direct.h>
  29.  #define GetDrive(d) _dos_getdrive(&d)
  30.  #define SetDrive(d) {unsigned x;_dos_setdrive(d,&x);}
  31.  #define FAR _far
  32. #endif
  33.  
  34. int count=0;
  35.  
  36. main(int argc, char *argv[])
  37. {
  38.       char *curdir,
  39.             sought[80],
  40.            *temp;
  41.       int   curdrive, newdrive, p;
  42.       void  searchdir(char *dir, char *ptrn);
  43.  
  44.       /*  Find out where we are */
  45.  
  46.       curdir=getcwd(NULL,80);
  47.       GetDrive(curdrive);
  48.  
  49.       /*  Find out what we're looking for  */
  50.  
  51.       if(argc>1)
  52.             strcpy(sought,argv[1]);
  53.       else
  54.       {
  55.             printf("\n\nPattern to search for: ");
  56.             gets(sought);
  57.       }
  58.  
  59.       /*  Get designator for another drive if specified  */
  60.  
  61.       if(sought[1]==':')
  62.       {
  63.             newdrive=(toupper(sought[0]))-64;       /* convert  */
  64.             SetDrive(newdrive);
  65.             p = (sought[2]=='\\') ? 3:2;
  66.             strcpy(sought, &(sought[p]));
  67.       }
  68.  
  69.       /*  Add wildcard prefix/suffix if necessary  */
  70.  
  71.       if(sought[0]=='.')
  72.       {
  73.             temp=strcat("*",sought);        /*  set prefix  */
  74.             strcpy(sought,temp);
  75.       }
  76.       if(!strchr(sought,'.'))
  77.             strcpy(sought,"*.*");           /*  set suffix  */
  78.  
  79.       /*  Perform search for pattern starting in root  */
  80.  
  81.       searchdir("\\",sought);
  82.       printf("\nNumber of matches: %d",count);
  83.  
  84.       /*  Restore Original Drive and Directory  */
  85.  
  86.       SetDrive(curdrive);
  87.       chdir(curdir);
  88.       return EXIT_SUCCESS;
  89. }
  90.  
  91. /*------------------------------------------------------------------------- */
  92.  
  93. void searchdir(char *path, char *ptrn)
  94. #define ANYFILE 0xFF                         /*  recursive routine  */
  95. {
  96.       struct find_t *f;
  97.       char          *wholepath;
  98.       unsigned      rtn;
  99.  
  100.       chdir(path);                    /*  change to new path  */
  101.       wholepath=getcwd(NULL,80);      /*  get full path name  */
  102.       f=malloc(sizeof(*f));
  103.  
  104.       /*  Search for filename matches in this directory  */
  105.  
  106.       rtn= _dos_findfirst(ptrn,ANYFILE,f);
  107.       while(rtn==0)
  108.       {
  109.             if( f->attrib != _A_SUBDIR )
  110.                   printf("%s\\%s\n",wholepath,f->name);
  111.             else  printf("%s\\%s <DIR>\n",wholepath, f->name);
  112.             ++count;
  113.  
  114.             rtn = _dos_findnext(f);         /* find next match  */
  115.       }  /* end while loop  */
  116.  
  117.       /*  Now search any subdirectories under this directory  */
  118.  
  119.       rtn= _dos_findfirst("*.*", _A_SUBDIR,f);
  120.       while(rtn==0)
  121.       {
  122.             if( (f->attrib == _A_SUBDIR) && (f->name[0] != '.'))
  123.             {
  124.                   searchdir(f->name,ptrn);   /* recursive call */
  125.                   chdir(wholepath);
  126.             }
  127.             rtn = _dos_findnext(f);  /* search next dir  */
  128.       }
  129.  
  130.       free(wholepath);
  131.       free(f);
  132. }
  133.